﻿package ua.mihaylenko.canvas.utils 
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import ua.mihaylenko.canvas.common.DocumentClass;
	import ua.mihaylenko.canvas.data.GlobalSettings;
	/**
	 * ...
	 * @author Mihayelnko A.L.
	 */
	public dynamic class Preloader extends Sprite
	{
		public var _smcTotalBar:Sprite;
		public var _stfPercentLoaded:TextField;
		private var _sLabel:String="";
		private static var _cInstance:Preloader;
		public function Preloader() 
		{
			_stfPercentLoaded.mouseEnabled = false;
		}
		public static function getInstance():Preloader {
			if (!_cInstance)_cInstance = new Preloader();
			return _cInstance;
		}
		public function set label(sLabel:String) {
			_sLabel = sLabel;
		}
		public function set progress(nProgress:Number) {
			if (!DocumentClass.AppStage.contains(this)) {
				this.x = GlobalSettings.PRELOADER_START_POSITION.x;
				this.y = GlobalSettings.PRELOADER_START_POSITION.y;
				DocumentClass.AppStage.addChild(this);
			}
			_smcTotalBar.scaleX = nProgress;
			if(_sLabel!=""){
				_stfPercentLoaded.text = _sLabel + " " + Math.floor(nProgress * 100)  + " %";
			}else {
				_stfPercentLoaded.text = Math.floor(nProgress * 100)  + " %";
			}
			if (nProgress == 1) {
				DocumentClass.AppStage.removeChild(this);
				_sLabel = "";
			}
		}
	}
	
}